package com.neusoft.a312cxy.android18cxy.model;

import com.neusoft.a312cxy.android18cxy.Interface.ShaiTuFace;
import com.neusoft.a312cxy.android18cxy.Listener.PostListener;
import com.neusoft.a312cxy.android18cxy.Listener.ShaiTuListListener;
import com.neusoft.a312cxy.android18cxy.bean.ShaiTuListBean;
import com.neusoft.a312cxy.android18cxy.service.ShaiTuService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Young on 2017/9/29.
 */


public class ShaiTuModel implements ShaiTuFace {

    private Retrofit retrofit;
    private ShaiTuService ShaiTuService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public ShaiTuModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())

                .build();
    }

    /**
     * 获得培训列表
     * @param SessionId 个人id账号
     * @param page 指定是哪一个页面
     * @param listListener 监听对象
     */
    @Override
    public void getList(String SessionId, int page,final ShaiTuListListener listListener) {
        ShaiTuService = retrofit.create(ShaiTuService.class);
        Call<List<ShaiTuListBean>> call =ShaiTuService.getShaiTuList(SessionId,page);
        call.enqueue(new Callback<List<ShaiTuListBean>>() {
            @Override
            public void onResponse(Call<List<ShaiTuListBean>> call, Response<List<ShaiTuListBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("onresponse fail");
                }
            }

            @Override
            public void onFailure(Call<List<ShaiTuListBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


    public Call getShaiTuInfo(String shaitu_id)
    {
        ShaiTuService ShaiTuService=retrofit.create(ShaiTuService.class);
        return ShaiTuService.getShaiTuInfo(1,shaitu_id);
    }

    public void  upcomment(String shaitu_id,String userid,String reply_userid,String reply ,final PostListener postListener){
        ShaiTuService shaiTuService=retrofit.create(ShaiTuService.class);
        Call<String> call = shaiTuService.getshaireply(shaitu_id,userid,reply_userid,reply);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&& response.body()!=null){

                    postListener.onResponse(response.body());
                }
                else postListener.onFail("OnResponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                postListener.onFail(t.toString());
            }
        });
    }

    public void  addpic(String thumb,String userid,String title,String club_id ,final PostListener postListener){
        ShaiTuService shaiTuService=retrofit.create(ShaiTuService.class);
        Call<String> call = shaiTuService.addpic(thumb,userid,title,club_id);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful()&& response.body()!=null){

                    postListener.onResponse(response.body());
                }
                else postListener.onFail("OnResponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                postListener.onFail(t.toString());
            }
        });
    }


}
